package app

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

// 0-华为 1-蓝思科技 2-京东方
var company_name = []string{"huawei", "lansikeji", "jingdongfang"}
var company_addr = []string{"0x763af809531e14c7f7a02e82eb7e067b941c7a85", "0x0b00b9f5aa7d4b0c1570493febc4f614c879ee74", "0x30651aa285caafd8dc47788cc2fa660065cd841d"}
var bank_addr = "0xc332d654bbdbecdba65e686cac399eb391901f77"

// common
var body_format = "{ \"groupId\" :1, \"signUserId\": \"test\", \"contractAbi\":[{\"constant\":false,\"inputs\":[{\"name\":\"increase_company_address\",\"type\":\"address\"},{\"name\":\"increase_company_amount\",\"type\":\"uint256\"}],\"name\":\"CompanyMoneyAmountIncrease\",\"outputs\":[{\"name\":\"ok\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from_address\",\"type\":\"address\"},{\"name\":\"to_address\",\"type\":\"address\"},{\"name\":\"money_amount\",\"type\":\"uint256\"},{\"name\":\"time_boundry\",\"type\":\"uint256\"}],\"name\":\"CreateRecipy\",\"outputs\":[{\"name\":\"ok\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"decrease_company_address\",\"type\":\"address\"},{\"name\":\"decrease_company_amount\",\"type\":\"uint256\"}],\"name\":\"CompanyMoneyAmountDecrease\",\"outputs\":[{\"name\":\"ok\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"}],\"name\":\"GetAllReceipy\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from_address\",\"type\":\"address\"},{\"name\":\"to_address\",\"type\":\"address\"},{\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"PayReceipy\",\"outputs\":[{\"name\":\"ok\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from_address\",\"type\":\"address\"},{\"name\":\"to_address\",\"type\":\"address\"},{\"name\":\"amount\",\"type\":\"uint256\"},{\"name\":\"time_boundry\",\"type\":\"uint256\"}],\"name\":\"BankHelpCompany\",\"outputs\":[{\"name\":\"ok\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from_address\",\"type\":\"address\"},{\"name\":\"origin_to_address\",\"type\":\"address\"},{\"name\":\"new_to_address\",\"type\":\"address\"},{\"name\":\"amount\",\"type\":\"uint256\"},{\"name\":\"time_boundry\",\"type\":\"uint256\"}],\"name\":\"TransferReceipy\",\"outputs\":[{\"name\":\"ok\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"},{\"name\":\"company_name\",\"type\":\"string\"},{\"name\":\"money_amount\",\"type\":\"uint256\"}],\"name\":\"CreateBankAccount\",\"outputs\":[{\"name\":\"ok\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"company_address\",\"type\":\"address\"}],\"name\":\"SearchMoneyAmount\",\"outputs\":[{\"name\":\"money_amount\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}], \"contractAddress\":\"0x6afb0665ae61fde96e1e57086d7a546b46e98e2a\", \"funcName\":\"%s\", \"funcParam\":%s }"

// Company_Name2ID: return -1 if illegal name
func Company_Name2ID(name string) int {
	id := -1

	for i, com := range company_name {
		if com == name {
			id = i
			break
		}
	}

	return id
}

// Company_Name2ID: return "" if illegal id
func Company_ID2Name(id int) string {
	if id > 2 {
		return ""
	} else {
		return company_name[id]
	}
}

// Company_Name2Addr: return "" if illegal name
func Company_Name2Addr(name string) string {
	id := -1

	for i, com := range company_name {
		if com == name {
			id = i
			break
		}
	}

	if id == -1 {
		return ""
	}
	return company_addr[id]
}

// Company_Name2Addr: return -1 if illegal addr
func Company_Addr2ID(addr string) int {
	id := -1

	for i, com := range company_addr {
		if com == addr {
			id = i
			break
		}
	}

	return id
}

func Company_SearchMoneyAmount(company_id int) string {
	funcName := "SearchMoneyAmount"
	param := fmt.Sprintf("[\"%s\"]", company_addr[company_id])

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

func Company_GetAllReceipy(company_id int) string {
	funcName := "GetAllReceipy"
	param := fmt.Sprintf("[\"%s\"]", company_addr[company_id])

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

func Company_CompanyMoneyAmountIncrease(company_id int, num int) string {
	funcName := "CompanyMoneyAmountIncrease"
	param := fmt.Sprintf("[\"%s\", %d]", company_addr[company_id], num)

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

func Company_CompanyMoneyAmountDecrease(company_id int, num int) string {
	funcName := "CompanyMoneyAmountDecrease"
	param := fmt.Sprintf("[\"%s\", %d]", company_addr[company_id], num)

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

func Company_CreateRecipy(from_id int, to_id int, num int, time int) string {
	funcName := "CreateRecipy"
	param := fmt.Sprintf("[\"%s\", \"%s\", %d, %d]", company_addr[from_id], company_addr[to_id], num, time)

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

func Company_PayReceipy(from_id int, to_id int, num int) string {
	funcName := "PayReceipy"
	param := fmt.Sprintf("[\"%s\", \"%s\", %d]", company_addr[from_id], company_addr[to_id], num)

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

func Company_PayBank(from_id int, num int) string {
	funcName := "PayReceipy"
	param := fmt.Sprintf("[\"%s\", \"%s\", %d]", company_addr[from_id], bank_addr, num)

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);
	return Post([]byte(body))
}

func Company_TransferReceipy(from_id int, old_to_id int, new_to_id int, num int, time int) string {
	funcName := "TransferReceipy"
	param := fmt.Sprintf("[\"%s\", \"%s\", \"%s\", %d, %d]", company_addr[from_id], company_addr[old_to_id], company_addr[new_to_id], num, time)

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

func Company_BankHelpCompany(from_id int, to_id int, num int, time int) string {
	funcName := "BankHelpCompany"
	param := fmt.Sprintf("[\"%s\", \"%s\", %d, %d]", company_addr[from_id], company_addr[to_id], num, time)

	body := fmt.Sprintf(body_format, funcName, param)
	// fmt.Println("body = ", body);

	return Post([]byte(body))
}

// func Post(body []byte) string {
// 	url := "http://121.199.4.253:5002/WeBASE-Front/trans/handleWithSign"
// 	contentType := "application/json"
// 	req, err := http.NewRequest(`POST`, url, bytes.NewBuffer(body))
// 	req.Header.Add(`content-type`, contentType)
// 	if err != nil {
// 		panic(err)
// 	}
// 	defer req.Body.Close()

// 	client := &http.Client{Timeout: 5 * time.Second}
// 	resp, err := client.Do(req)
// 	if err != nil {
// 		panic(err)
// 	}
// 	defer resp.Body.Close()

// 	result, _ := ioutil.ReadAll(resp.Body)
// 	return string(result)
// }

func Post(body []byte) string {
	url := "http://121.199.4.253:5002/WeBASE-Front/trans/handleWithSign"
	contentType := "application/json"
	/*
		req, err := http.NewRequest(`POST`, url, bytes.NewBuffer(body))
		req.Header.Add(`content-type`, contentType)
		if err != nil {
			panic(err)
		}
		defer req.Body.Close()
	*/
	client := &http.Client{Timeout: 5 * time.Second}
	// resp, err := client.Do(req)
	resp, err := client.Post(url, contentType, bytes.NewBuffer(body))
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	result, _ := ioutil.ReadAll(resp.Body)

	fmt.Println("result", string(result))

	return string(result)
}
